package com.tulele.vtool.handler;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.tulele.vtool.model.BrandInfo;
import com.tulele.vtool.model.RevokeStickCommodityInfoReqBean;
import com.tulele.vtool.model.UserCommodityInfo;
import com.tulele.vtool.model.UserInfo;
import com.tulele.vtool.model.reqEntity.*;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.IUserBrandInfoService;
import com.tulele.vtool.service.IUserCommodityInfoService;
import com.tulele.vtool.service.IUserInfoService;
import com.tulele.vtool.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class UserCommodityHandler {

    @Autowired
    private IUserCommodityInfoService userCommodityInfoService;

    @Autowired
    private IUserBrandInfoService userBrandInfoService;

    @Autowired
    private IUserInfoService userInfoService;

    @PostMapping(value = "/createCommodityInfo", produces = "application/json;charset=UTF-8")
    public RespData createUserCommodityInfo(@RequestHeader("userId") String userId, @RequestBody CreateUserCommodityInfoReqBean createUserCommodityInfoReqBean) {

        String commodityPrice = createUserCommodityInfoReqBean.getCommodityPrice();
        if (StringUtils.isNotBlank(commodityPrice) && (!DateUtil.isNumDouble(commodityPrice)
                || commodityPrice.trim().length() >= 10)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("商品价格异常");
            return respData;
        }

        String originalCommodityPrice = createUserCommodityInfoReqBean.getOriginalCommodityPrice();
        if (StringUtils.isNotBlank(originalCommodityPrice) && (!DateUtil.isNumDouble(originalCommodityPrice)
                || originalCommodityPrice.trim().length() >= 10)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("商品价格异常");
            return respData;
        }


        UserCommodityInfo userCommodityInfo = new UserCommodityInfo();
        userCommodityInfo.setUserId(Integer.parseInt(userId));
        userCommodityInfo.setCommodityName(createUserCommodityInfoReqBean.getCommodityName());
        userCommodityInfo.setCommodityImgUrl(createUserCommodityInfoReqBean.getCommodityImgUrl());
        userCommodityInfo.setCommodityStatus(1);
        userCommodityInfo.setCommodityDesc(createUserCommodityInfoReqBean.getCommodityDesc());

        if (StringUtils.isNotBlank(commodityPrice)) {
            userCommodityInfo.setCommodityPrice(Double.valueOf(commodityPrice));
        }

        if (StringUtils.isNotBlank(originalCommodityPrice)) {
            userCommodityInfo.setOriginalCommodityPrice(Double.valueOf(originalCommodityPrice));
        }

        //获取选中微站信息
        BrandInfo brandInfo = userBrandInfoService.getUserSelectBrandInfo(Integer.parseInt(userId));
        if (brandInfo != null && brandInfo.getId() != null) {
            userCommodityInfo.setBrandId(brandInfo.getId());
        }

        userCommodityInfo.setCreateTime(DateUtil.getCurDate());

        userCommodityInfoService.createUserCommodityInfo(userCommodityInfo);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    @PostMapping(value = "/removeCommodityInfo", produces = "application/json;charset=UTF-8")
    public RespData removeCommodityInfo(@RequestHeader("userId") String userId, @RequestBody RemoveCommodityInfoReqBean commodityInfoReqBean) {

        if (commodityInfoReqBean == null || commodityInfoReqBean.getCommodityId() == null) {

            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;

        }

        userCommodityInfoService.pseudoDeleteCommodityInfo(commodityInfoReqBean.getCommodityId());

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    @PostMapping(value = "/getCommodityDetailInfo", produces = "application/json;charset=UTF-8")
    public RespData getCommodityDetailInfo(@RequestHeader("userId") String userId, @RequestBody GetCommodityDetailInfoReqBean getCommodityDetailInfoReqBean) {

        if (getCommodityDetailInfoReqBean == null || getCommodityDetailInfoReqBean.getCommodityId() == null) {

            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;

        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(10);

        UserCommodityInfo commodityInfo = userCommodityInfoService.getCommodityDetailInfo(getCommodityDetailInfoReqBean.getCommodityId());
        if (commodityInfo == null) {

            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setData(resultMap);
            respData.setMsg("该商品已被删除");
            return respData;

        }

        resultMap.put("commodityInfo", commodityInfo);

        Integer commodityUserId = commodityInfo.getUserId();

        UserInfo userInfo = userInfoService.getUserMainInfo(commodityUserId);
        //文章所有者的用户信息
        resultMap.put("userId", String.valueOf(userInfo.getUserId()));
        resultMap.put("nickName", userInfo.getNickName());
        resultMap.put("avatarUrl", userInfo.getAvatarUrl());
        resultMap.put("signature", userInfo.getSignature());

        resultMap.put("qrcodeUrl", userInfo.getQrcodeUrl());
        resultMap.put("telNum", userInfo.getTelNum());

        resultMap.put("curUserId", userId);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;

    }

    @PostMapping(value = "/getUserCommodityInfo", produces = "application/json;charset=UTF-8")
    public RespData getUserCommodityInfo(@RequestHeader("userId") String userId, @RequestBody GetUserCommodityInfoReqBean getUserCommodityInfoReqBean) {

        Integer pageNum = getUserCommodityInfoReqBean.getPageNum();
        if (pageNum == null || pageNum.intValue() <= 0) {
            pageNum = 1;
        }

        Integer pageSize = getUserCommodityInfoReqBean.getPageSize();
        if (pageSize == null || pageNum.intValue() <= 0) {
            pageSize = 10;
        }

        Integer finalUserId = getUserCommodityInfoReqBean.getOtherUserId();
        if (finalUserId == null) {
            finalUserId = Integer.parseInt(userId);
        }


        BrandInfo selectBrandInfo = userBrandInfoService.getUserSelectBrandInfo(finalUserId);
        Integer brandId = null;
        if (selectBrandInfo != null) {
            brandId = selectBrandInfo.getId();
        }
        PageInfo<UserCommodityInfo> userCommodityInfoPageInfo = userCommodityInfoService.getUserCommodityInfo(finalUserId, brandId, pageNum, pageSize);

        List<UserCommodityInfo> userCommodityInfoList = userCommodityInfoPageInfo.getList();
        // 20200429 yl 返回用户名 start
        String otherUserName = null;
        if (null != getUserCommodityInfoReqBean.getOtherUserId()){
            UserInfo userMainInfo = userInfoService.getUserMainInfo(getUserCommodityInfoReqBean.getOtherUserId());
            if (null !=userMainInfo){
                otherUserName = userMainInfo.getNickName();
            }
        }
        // 20200429 yl 返回用户名 end



        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(3);

        long count = userCommodityInfoPageInfo.getTotal();
        resultMap.put("count", count);
        resultMap.put("items", userCommodityInfoList);
        resultMap.put("otherUserName",otherUserName);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;

    }

    @PostMapping(value = "/modifyCommodityInfo", produces = "application/json;charset=UTF-8")
    public RespData modifyCommodityInfo(@RequestHeader("userId") String userId, @RequestBody ModifyCommodityInfoReqBean modifyCommodityInfoReqBean) {

        if (modifyCommodityInfoReqBean == null || modifyCommodityInfoReqBean.getCommodityId() == null) {

            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;

        }

        UserCommodityInfo userCommodityInfo = new UserCommodityInfo();
        userCommodityInfo.setId(modifyCommodityInfoReqBean.getCommodityId());
        userCommodityInfo.setCommodityImgUrl(modifyCommodityInfoReqBean.getCommodityImgUrl());
        userCommodityInfo.setModifyTime(DateUtil.getCurDate());
        userCommodityInfoService.modifyCommodityInfo(userCommodityInfo);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    @PostMapping(value = "/stickCommodityInfo", produces = "application/json;charset=UTF-8")
    public RespData stickCommodityInfo(@RequestHeader("userId") String userId, @RequestBody StickCommodityInfoReqBean stickCommodityInfoReqBean) {

        if (stickCommodityInfoReqBean == null || stickCommodityInfoReqBean.getCommodityId() == null) {

            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;

        }

        userCommodityInfoService.stickCommodityInfo(Integer.parseInt(userId), stickCommodityInfoReqBean.getCommodityId());

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    @PostMapping(value = "/revokeStickCommodityInfo", produces = "application/json;charset=UTF-8")
    public RespData revokeStickCommodityInfo(@RequestHeader("userId") String userId, @RequestBody RevokeStickCommodityInfoReqBean revokeStickCommodityInfoReqBean) {

        if (revokeStickCommodityInfoReqBean == null || revokeStickCommodityInfoReqBean.getCommodityId() == null) {

            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;

        }

        UserCommodityInfo userCommodityInfo = new UserCommodityInfo();
        userCommodityInfo.setId(revokeStickCommodityInfoReqBean.getCommodityId());
        userCommodityInfo.setCommodityStickStatus(0);
        userCommodityInfo.setModifyTime(DateUtil.getCurDate());
        userCommodityInfoService.modifyCommodityInfo(userCommodityInfo);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    @PostMapping(value = "/removeAllUserCommodityInfo", produces = "application/json;charset=UTF-8")
    public RespData removeAllUserCommodityInfo(@RequestHeader("userId") String userId) {

        userCommodityInfoService.removeAllUserCommodityInfo(Integer.parseInt(userId), 0);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;

    }

}
